package com.sf.fy.uav.applet.order.controller.task;

import com.sf.fy.uav.rest.SysMapRest;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.io.UnsupportedEncodingException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@RequestMapping("/sys/tencent/")
@Slf4j
public class TencentApiController {
    @Value("${new-proxy-url:IE-UOCS-CORE-3adm832e.lbsit.sfcloud.local}")
    private String newProxyUrl;

    private final SysMapRest sysMapRest;

    private final static Logger logger = LoggerFactory.getLogger(TencentApiController.class);

    @GetMapping("get-tencent-map")
    public String queryMap(@RequestParam String key, @RequestParam String keyword, @RequestParam String boundary, @RequestParam String page_index, @RequestParam String page_size) {
        String forObject = "";
        try {
            Map<String, String> params = new HashMap<>(10);
            params.put("param1", key);
            params.put("param2", URLEncoder.encode(keyword, "UTF-8"));
            params.put("param3", boundary);
            params.put("param4", page_index);
            params.put("param5", page_size);
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(newProxyUrl, 80));
            SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
            requestFactory.setProxy(proxy);
            RestTemplate restTemplate = new RestTemplate(requestFactory);
            forObject = restTemplate.getForObject("https://apis.map.qq.com/ws/place/v1/search?key={param1}&keyword={param2}&boundary={param3}&page_index={param4}&page_size={param5}", String.class, params);
            logger.info(forObject);
        } catch (UnsupportedEncodingException e) {
            logger.info("{报错信息}：", e);
        }
        return forObject;
    }

    @GetMapping("get-tencent-map-orientation")
    public String queryOrientation(@RequestParam String key) {
        Map<String, String> params = new HashMap<>(1);
        params.put("param1", key);
        params.put("param2", "183.62.127.26");
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(newProxyUrl, 80));
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setProxy(proxy);
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        String forObject = restTemplate.getForObject("https://apis.map.qq.com/ws/location/v1/ip?key={param1}&ip={param2}", String.class, params);
        logger.info(forObject);
        return forObject;
    }

    @GetMapping("get-tencent-map-geocoder")
    public String queryGeocoder(@RequestParam String key, @RequestParam String location) {
        Map<String, String> params = new HashMap<>(2);
        params.put("param1", key);
        params.put("param2", location);
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(newProxyUrl, 80));
        SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
        requestFactory.setProxy(proxy);
        RestTemplate restTemplate = new RestTemplate(requestFactory);
        String forObject = restTemplate.getForObject("https://apis.map.qq.com/ws/geocoder/v1/?location={param2}&key={param1}&get_poi=1", String.class, params);
        logger.info(forObject);
        return forObject;
    }

    @GetMapping("get-tencent-map-address")
    public String queryAddress(@RequestParam String key, @RequestParam String address) {
        String forObject = "";
        try {
            Map<String, String> params = new HashMap<>(2);
            params.put("param1", key);
            params.put("param2", URLEncoder.encode(address, "UTF-8"));
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(newProxyUrl, 80));
            SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
            requestFactory.setProxy(proxy);
            RestTemplate restTemplate = new RestTemplate(requestFactory);
            forObject = restTemplate.getForObject("https://apis.map.qq.com/ws/geocoder/v1/?address={param2}&key={param1}", String.class, params);
            logger.info(forObject);
        } catch (UnsupportedEncodingException e) {
            logger.info("{报错信息}：", e);
        }
        return forObject;
    }

    @GetMapping("get-location-map")
    public String getLocationMapByIp(HttpServletRequest request) {
        return sysMapRest.getLocationByIp(request);
    }

}
